import request from '~/utils/http'
import type { ApiResponse, LoginParams, UserInfo, EnterpriseInfo } from '~/types/api'

export const landlordLoginApi = (data: any, isLoading?: boolean): Promise<ApiResponse> => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/landlordLogin/login`, isLoading, {
    method: 'POST',
    data: data
  })
}

export const oauth2PasswordApi = async (data: LoginParams, isLoading?: boolean): Promise<ApiResponse> => {
  const config = useRuntimeConfig()
  const { sha256 } = await import('js-sha256')
  const newData = {
    ...data,
    password: sha256(data.password)
  }
  return request(`${config.public.apiBase}/ezsafe-server/oauth2/password`, isLoading, {
    method: 'POST',
    data: newData,
    type: 'formData',
    isSha256: true
  })
}

export const authorityMeApi = (isLoading?: boolean): Promise<ApiResponse<UserInfo>> => {
  const config = useRuntimeConfig()
  return request(`${config.public.cloudRbacUrl}/authority/me`, isLoading, {
    method: 'GET'
  })
}

export const getUserRoleApi = (isLoading?: boolean): Promise<ApiResponse<string[]>> => {
  const config = useRuntimeConfig()
  return request(`${config.public.cloudRbacUrl}/authority/role_signs`, isLoading, {
    method: 'GET'
  })
}

export const userAccountStateApi = (dlm: string, isLoading?: boolean): Promise<ApiResponse> => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/basicInfo/getByDlm?dlm=${dlm}`, isLoading, {
    method: 'GET'
  })
}

// 企业基础信息
export const basicInfoApi = (data: any, isLoading?: boolean): Promise<ApiResponse<{ content: EnterpriseInfo[] }>> => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/basicInfo/queryByCondition`, isLoading, {
    method: 'POST',
    data: data
  })
}

// 保存登录人微信ID
export const saveWXInfoApi = (data: any, isLoading?: boolean): Promise<ApiResponse> => {
  const config = useRuntimeConfig()
  return request(`${config.public.apiBase}/ezsafe-server/xcxdlry/save`, isLoading, {
    method: 'POST',
    data: data
  })
}
